<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Drop Page
    *
    * @author Thomas Brandstätter <tb.essentialmind.com>
    * @version n/a
    * @copyright essentialmind.com 2010
    *
    *********************************************************/

    /***
    * class ajaxDropPage
    ****
    * This class will drop a page reference via an AJAX method
    ****
    * @package ajax
    * @author Thomas Brandstätter <tb.essentialmind.com>
    * @version n/a
    * @copyright essentialmind.com 2010
    ***/
    class ajaxClonePage extends ajaxAction{

        public function process(){
            $id = 		(int)$_REQUEST['id'];
            
            // try to copy the page
            try{
                
                
                //fetch origin row
                $origin = $this->db->getRow("SELECT * FROM pagemap WHERE id = " . $id);

                $newURL = $origin['pageurl']."-clone";
                $isPresent = $this->db->getOne("SELECT * FROM pagemap 
                                                WHERE nodeid = " .(int)$origin['nodeid'] . " AND pageurl = '" . $newURL . "'" );
                
                if ($isPresent != nil) {
                    $newURL = $newURL . time();
                }
                
				if ($GLOBALS['_CONFIG']['enable_order_position']) {
	                $nextPos = $this->db->getOne("SELECT max(order_pos) FROM pagemap ");
	                $nextPos++;
                
					$this->db->Execute("INSERT INTO pagemap(nodeid,pageurl,pageid,pagetype,title, owner_gid, owner_uid, order_pos) VALUES(
		                                                                            '".(int)$origin['nodeid']."',
		                                                                            '".$newURL."',
		                                                                            '".(int)$origin['pageid']."',
		                                                                            '".(int)$origin['pagetype']."',
		                                                                            '".$origin['title']."-Clone',
		                                                                            ".$origin['owner_gid'].",
		                                                                            ".$origin['owner_uid'].",
																					".(int)$nextPos."
		                                                                        )");
				} else {
				 	$this->db->Execute("INSERT INTO pagemap(nodeid,pageurl,pageid,pagetype,title, owner_gid, owner_uid) VALUES(
		                                                                            '".(int)$origin['nodeid']."',
		                                                                            '".$newURL."',
		                                                                            '".(int)$origin['pageid']."',
		                                                                            '".(int)$origin['pagetype']."',
		                                                                            '".$origin['title']."-Clone',
		                                                                            ".$origin['owner_gid'].",
		                                                                            ".$origin['owner_uid']."
		                                                                        )");
				}

               // fetch new ID                                                             
			    $newPageID = $this->db->getOne("SELECT last_value FROM pagemap_id_seq");
			    
			    // to be sure we fetch correct data and joined attributes 
                $inserted = $this->db->getRow("SELECT pm.*, d.name AS datatype FROM pagemap as pm
                                                LEFT JOIN datatype as d ON (pm.pagetype = d.id) 
                                                WHERE pm.id = " . $newPageID);

			    // assign to smarty
                $this->smarty->assign('newPage', $inserted);
                
                //log action
                $this->log->recordAction("Ajax: Clone page (".$id.")");
                
                $this->r->html = $this->smarty->preFetch('ajax'.DIRECTORY_SEPARATOR.'insertPageRow.html');
                $this->r->id = $newPageID;
                
            //something went wrong
            }catch(exception $e){
                //log error
                $this->log->recordError($e->getMessage());
            
                //do nothing
            }
        }
    }
?>